**//Pocketbook and Sociotropic Economic Voting: How Does Inflation Affect Voting Decisions? (ANES 2024 PILOT Dataset, thank you ANES TEAM!!)


//Muhammad Hassan Bin Afzal
//Department of Political Science and Public Service
//University of Tennessee at Chattanooga
//WPF218@Tennessee.edu 
//https://orcid.org/0000-0001-8192-0885 

//Dear User: Please download the original dataset from (https://electionstudies.org/data-center/), please download and 2024 pilot study dataset. And The do file should work. Let me know if you need some help or assistance. Look for ward to your collaboration.//**


**DV1: Likely to Vote the Incumbment in upcoming election while facing sever economic hardship (Pocketbook Economic Voting) or Inflation behavior index**

* Likely to vote in 2024 election Remove 'No Answer' and 'Inapplicable' responses
drop if vchoice_rematch == -7 | vchoice_rematch == -1

* Verify the cleaned variable
tabulate vchoice_rematch

* Recode the 'vchoice_rematch' variable
recode vchoice_rematch (1=0) (2=1), gen(vote_biden_trump)
label variable vote_biden_trump "Vote Choice: 0=Trump, 1=Biden"
tab vote_biden_trump

**Primary IV: Inflation behavior Index reflecting economic hardships faced in last year (Pocketbook)**

**Inflation Behavior Index** ** Creating IBR Index (main IV for Pocketbook voting)**

* Remove cases where the variables were inapplicable
drop if infl_behav_1 == -1 | infl_behav_2 == -1 | infl_behav_3 == -1 | infl_behav_4 == -1

* Recode and label infl_behav_1
recode infl_behav_1 (2 = 0 "Did not engage") (1 = 1 "Did engage"), gen(borrow_money)
label variable borrow_money "Borrowed money to pay bills in the past year"

* Recode and label infl_behav_2
recode infl_behav_2 (2 = 0 "Did not engage") (1 = 1 "Did engage"), gen(cut_spending)
label variable cut_spending "Cut down on everyday spending in the past year"

* Recode and label infl_behav_3
recode infl_behav_3 (2 = 0 "Did not engage") (1 = 1 "Did engage"), gen(cancel_purchases)
label variable cancel_purchases "Cancelled or postponed major purchases in the past year"

* Recode and label infl_behav_4
recode infl_behav_4 (2 = 0 "Did not engage") (1 = 1 "Did engage"), gen(dip_savings)
label variable dip_savings "Dipped into savings in the past year"

* Check the new variables
tabulate borrow_money
tabulate cut_spending
tabulate cancel_purchases
tabulate dip_savings

* Exploratory Factor Analysis
factor borrow_money cut_spending cancel_purchases dip_savings, pcf

* Confirmatory Factor Analysis
* Assuming the structure from EFA suggests a single factor
sem (Inflation_Behavior -> borrow_money cut_spending cancel_purchases dip_savings)

* Create an index of inflation behavior
gen inflation_behavior_index = borrow_money + cut_spending + cancel_purchases + dip_savings
label variable inflation_behavior_index "Inflation behavior index (sum of behaviors engaged in)"

* Check the new index variable
summarize inflation_behavior_index
tabulate inflation_behavior_index

* Check internal consistency using Cronbach's Alpha
alpha borrow_money cut_spending cancel_purchases dip_savings

* Check the correlation between the items
pwcorr borrow_money cut_spending cancel_purchases dip_savings, sig

* Check the variance of the index
summarize inflation_behavior_index, detail
**Data cleaned and code works perfectly, make sure to download the main dataset and run the above coding sceme to build the inflation behavior index**


**DV2: (main IV for Sociotropic voting)Likely to Vote the Incumbment in upcoming election while thinking the incumbent party is NOT handling the cost and daily economy party better**
* Create and label the dummy variable
gen dem_not_econ = 0
replace dem_not_econ = 1 if own_costliv == 2
label variable dem_not_econ "Dems are not handling the national economy better"
label define economy_lbl 0 "No" 1 "Yes"
label values dem_not_econ economy_lbl

* Verify the new variable
tab dem_not_econ

**IV2--Gender**
**1=male 2=female
tab gender
gen log_age = log(age)

* Recode faminc_new to combine income categories above $150,000 and remove "Prefer not to say" and "No Answer"
drop if faminc_new ==97 | faminc_new ==-7
recode faminc_new (12/16=12), gen(income)
label define faminc_lbl_recoded1 ///
    1 "Less than $10,000" ///
    2 "$10,000 - $19,999" ///
    3 "$20,000 - $29,999" ///
    4 "$30,000 - $39,999" ///
    5 "$40,000 - $49,999" ///
    6 "$50,000 - $59,999" ///
    7 "$60,000 - $69,999" ///
    8 "$70,000 - $79,999" ///
    9 "$80,000 - $99,999" ///
    10 "$100,000 - $119,999" ///
    11 "$120,000 - $149,999" ///
    12 "$150,000 and above"
label values income faminc_lbl_recoded1
tab income



**Create Binary Party ID"**
tab pid3
gen democrat = (pid3 == 1)
gen republican = (pid3 == 2)
gen independent = (pid3 == 3)
tab democrat
tab republican
tab independent


**Race Recode
* Step 1: Create a New Variable
* Recode race variable
generate White = (race4 == 1)
generate Black = (race4 == 2)
generate Hispanic = (race4 == 3)

tab race4
tab White
tab Black
tab Hispanic

* Create binary variable for being a student
gen student = (employ == 8)
label define student_lbl 0 "No" 1 "Yes"
label values student student_lbl
label variable student "Are you a student?"
tab student

* Create binary variable for being full-time employed
gen full_time = (employ == 1)
label define full_time_lbl 0 "No" 1 "Yes"
label values full_time full_time_lbl
label variable full_time "Are you full-time employed?"
tab full_time

* Create binary variable for being part-time employed
gen part_time = (employ == 2)
label define part_time_lbl 0 "No" 1 "Yes"
label values part_time part_time_lbl
label variable part_time "Are you part-time employed?"
tab part_time

* Create binary variable for being unemployed
gen unemployed = (employ == 4)
label define unemployed_lbl 0 "No" 1 "Yes"
label values unemployed unemployed_lbl
label variable unemployed "Are you unemployed?"
tab unemployed

* Create binary variable for being retired
gen retired = (employ == 5)
label define retired_lbl 0 "No" 1 "Yes"
label values retired retired_lbl
label variable retired "Are you retired?"
tab retired

* Create dummy variable for living in a big city
gen big_city = 0
replace big_city = 1 if urbanicity2 == 1
label variable big_city "Lives in a big city"

**Interaction terms Inflation Index and Being self-Identified as a Democrat (Pocketbook Economic Voting)**

* Create the interaction term variable
gen demIBR = democrat * inflation_behavior_index
label variable demIBR "Interaction: Democrat * Inflation Behavior Index"
//Verify the new variable
summarize demIBR
tab demIBR
**Interaction terms between thinking the incumbeint party is not handling the national economy and cost of economy well and Being self-Identified as a Democrat (sociotropic Economic Voting)**
* Create the interaction term variable
gen dem_not_econ_dem = dem_not_econ * democrat
label variable dem_not_econ_dem "Interaction: Dem not handling econ * Democrat"

* Verify the new variable
tab dem_not_econ_dem

//***Remove all missing cases;
gen complete_case = !missing(vote_biden_trump, dem_not_econ, inflation_behavior_index, gender, log_age, educ, income, White, Black, Hispanic, republican, democrat, independent, student, full_time, part_time, unemployed, retired, big_city, demIBR, dem_not_econ_dem)


* Model 1: Likelihood for voting for the incumbent experiencing severe economic hardship (Bivariate-Pocketbook)**
logit vote_biden_trump inflation_behavior_index if complete_case [pweight=weight],or
estimates store Model1

* Model 2: Likelihood for voting for the incumbent experiencing severe economic hardship (Multivariate-ctrl variables-Pocketbook)**
logit vote_biden_trump inflation_behavior_index gender log_age educ income White Black hispanic democrat republican student full_time part_time unemployed retired big_city if complete_case [pweight=weight],or
estimates store Model2
* Model 3: Likelihood for voting for the incumbent while experiencing severe economic hardship (Multivariate-ctrl variables interactionterms-Pocketbook)**
logit vote_biden_trump inflation_behavior_index gender log_age educ income White Black hispanic democrat republican student full_time part_time unemployed retired big_city demIBR if complete_case [pweight=weight],or
estimates store Model3

* Model 4: Likelihood for voting for the incumbent perceving the incumbent party is not handling economy better (Bivariate-Sociotropic)**
logit vote_biden_trump  dem_not_econ if complete_case [pweight=weight],or
estimates store Model4
* Model 5: Likelihood for voting for the incumbent perceving the incumbent party is not handling economy better (Multivariate with CTRL-Sociotropic)**
logit vote_biden_trump dem_not_econ gender log_age educ income White Black hispanic democrat republican student full_time part_time unemployed retired big_city if complete_case [pweight=weight],or
estimates store Model5
* Model 6: Likelihood for voting for the incumbent perceving the incumbent party is not handling economy better ( CTRL and Interaction-Sociotropic)**
logit vote_biden_trump dem_not_econ gender log_age educ income White Black hispanic democrat republican student full_time part_time unemployed retired big_city dem_not_econ_dem  if complete_case [pweight=weight],or
estimates store Model6
* Output the results side by side with AIC, BIC, and other statistics
esttab Model1 Model2 Model3 Model4 Model5 Model6, eform cells(b(star fmt(3)) se(par fmt(3))) stats(N ll bic aic r2_p, fmt(0 3 3 3 3) labels("Observations" "Log likelihood" "BIC" "AIC" "Pseudo R-squared")) legend collabels(none) varlabels(_cons "Constant") starlevels(* 0.05 ** 0.01 *** 0.001)

**XX----------------------------------------------------------------------XX**
**XX----------------------------------------------------------------------XX**

**Set of Coefficient Plots**
* Generate first coefplot for Model 1, 2, and 3 side by side
* Generate coefplot for Model 1, 2, and 3 with colors and p-values
coefplot (Model1, label("Model 1: Bivariate Pocketbook") mcolor(blue)) ///
         (Model2, label("Model 2: Multivariate Pocketbook") mcolor(red)) ///
         (Model3, label("Model 3: Interaction Pocketbook") mcolor(green)), ///
         xline(1, lcolor(black)) ///                           // Add vertical line at 1
         ciopts(lcolor(blue red green) lwidth(medium)) ///      // Customize confidence intervals
         legend(on) ///                                         // Turn on legend
         title("Likelihood of Voting for Incumbent (Pocketbook)") ///
         drop(_cons) ///                                        // Drop the constant term
         mlabel(cond(@pval<0.001, "***", cond(@pval<0.016, "**", cond(@pval<0.05, "*", "")))) /// // Show p-values as stars
         mlabpos(12) ///                                        // Position the stars
         scheme(white_tableau)                                  // Clean and professional theme
      

* Generate second coefplot for Model 4, 5, and 6 side by side
coefplot (Model4, label("Model 4: Bivariate Sociotropic") mcolor(blue)) ///
         (Model5, label("Model 5: Multivariate Sociotropic") mcolor(red)) ///
         (Model6, label("Model 6: Interaction Sociotropic") mcolor(green)), ///
         xline(1, lcolor(black)) ///                           // Add vertical line at 1
         ciopts(lcolor(blue red green) lwidth(medium)) ///      // Customize confidence intervals
         legend(on) ///                                         // Turn on legend
         title("Likelihood of Voting for Incumbent (Sociotropic)") /// 
         drop(_cons) ///                                        // Drop the constant term
         mlabel(cond(@pval<0.001, "***", cond(@pval<0.016, "**", cond(@pval<0.05, "*", "")))) /// // Show p-values as stars
         mlabpos(12) ///                                        // Position the stars
         scheme(white_tableau)                                  // Clean and professional theme




**Check Individual Behavior VS Composite Behavior**


* Define the dependent variable
global dv vote_biden_trump

* Define the individual behavior variables
global behaviors "borrow_money cut_spending cancel_purchases dip_savings"

* Create a local to store p-values
local p_values

* Logistic regression for each individual behavior
foreach behavior of global behaviors {
    logit $dv `behavior' [pweight=weight], or
    estimates store Model`behavior'
    
    * Extract p-value
    matrix M = r(table)
    local p_value = M[4,1]
    local p_values `p_values' `behavior'=`p_value'
}

* Logistic regression for cumulative behavior index
logit $dv inflation_behavior_index [pweight=weight], or
estimates store ModelInflationBehaviorIndex

* Extract p-value for the cumulative behavior index
matrix M = r(table)
local p_value = M[4,1]
local p_values `p_values' inflation_behavior_index=`p_value'

* Display stored p-values
di "`p_values'"

* Margins and plots for each individual behavior
foreach behavior of global behaviors {
    quietly logit $dv `behavior' [pweight=weight], or
    margins, at(`behavior'=(0 1))
    local p_value = substr("`p_values'", strpos("`p_values'", "`behavior'=") + strlen("`behavior'="), 6)
    marginsplot, title("Inflation behavior~ Voting4Incumbent") note("p-value: `p_value'", position(3)) name(Graph_`behavior', replace)
}

* Margins and plot for cumulative behavior index
quietly logit $dv inflation_behavior_index [pweight=weight], or
margins, at(inflation_behavior_index=(0 1 2 3 4))
local p_value = substr("`p_values'", strpos("`p_values'", "inflation_behavior_index=") + strlen("inflation_behavior_index="), 6)
marginsplot, title("Effect-Index-Voting4Incumbent") note("p-value: `p_value'", position(3)) name(Graph_inflation_behavior_index, replace)

* Combine all margins plots into one screen
graph combine Graph_borrow_money Graph_cut_spending Graph_cancel_purchases Graph_dip_savings Graph_inflation_behavior_index, title("Pocketbook Economic Behavior~ Voting4Incumbent")

* Export the combined plot
graph export "combined_marginsplot.png", as(png) replace

* Tabulate individual behavior variables
tabulate borrow_money
tabulate cut_spending
tabulate cancel_purchases
tabulate dip_savings

* Tabulate voting intention
tabulate $dv

* Tabulate inflation behavior index
tabulate inflation_behavior_index


**XX----------------------------------------------------------------------XX**
* Run the full logistic regression model (Pocketbook Economic Voting)
logit vote_biden_trump inflation_behavior_index gender log_age educ income White Black hispanic democrat republican student full_time part_time unemployed retired big_city demIBR if complete_case [pweight=weight],or

* Create margins plot for the bivariate effect of education on likely voting for the incumbent
margins, at(inflation_behavior_index=(0 1 2 3 4))
marginsplot, ytitle("Pr(Vote for Incumbent)") xtitle("Inflation Behavior Index (IBR)") title("IBR and Voting Behavior") name(plot1, replace)

* Create margins plot for the effect of being a Democrat on likely voting for the incumbent
margins, at(democrat=(0 1))
marginsplot, ytitle("Pr(Vote for Incumbent)") xtitle("Democrat (0=No, 1=Yes)") title("Being Democrat & Likely Vote for Incumbent") name(plot2, replace)

* Create margins plot for the interaction effect of education and being a Democrat on likely voting for the incumbent
margins, at(educ=(1 2 3 4 5) democrat=(0 1))
marginsplot, ytitle("Pr(Vote for Incumbent)") xtitle("Education Level") title("Interaction: Education & Being Democrat on Likely Vote for Incumbent") name(plot3, replace)

* Create margins plot for the effect of being a Democrat with the inflation behavior index
margins, at(demIBR=(0 1 2 3 4))
marginsplot, ytitle("Pr(Vote for Incumbment)") xtitle("Infl. Index Interacting with being Dem") title("Dems Likely Vote4Incumbment") name(plot4, replace)

* Combine all three plots into one screen
graph combine plot1 plot2 plot3 plot4, col(2) row(4) title("Effects of Education and Being Democrat on Likely Voting for Incumbent")

**XX----------------------------------------------------------------------XX**


**XX----------------------------------------------------------------------XX**
* Run the logistic regression model (Sociotropic Economic Voting)
logit vote_biden_trump dem_not_econ gender log_age educ income White Black hispanic democrat republican student full_time part_time unemployed retired big_city dem_not_econ_dem  if complete_case [pweight=weight],or

* Create margins plot for the bivariate effect of likely to vote for Biden while perceiving the Incumbent Party not handling the US Economy Well**
margins, at(dem_not_econ=(0 1))
marginsplot, ytitle("Pr(Vote for Biden)") xtitle("Dems not handling Economy Better") title("US Econ. Perception Likely Vote4Incumbment") name(plot1, replace)

* Create margins plot for the effect of being a Democrat on likely voting for the incumbent
margins, at(democrat=(0 1) dem_not_econ=(0 1))
marginsplot, ytitle("Pr(Vote for Incumbent)") xtitle("Democrat (0=No, 1=Yes)") title("Being Democrat & Likely Vote4Incumbment") name(plot2, replace)

* Create margins plot for the effect of increasing level of education with US National Economy Perception
margins, at(educ=(1 2 3 4 5) dem_not_econ=(0 1))
marginsplot, ytitle("Pr(Vote for Biden)") xtitle("Education Level") title("Education & Likely Vote4Incumbment") name(plot3, replace)

* Create margins plot for the effect of being a Democrat with US National Economy Perception
margins, at(dem_not_econ_dem=(0 1))
marginsplot, ytitle("Pr(Vote for Biden)") xtitle("Self-identified Democrat") title("Dems Likely Vote4Incumbment") name(plot4, replace)

* Combine all four plots into one screen
graph combine plot1 plot2 plot3 plot4, col(2) row(2) title("US Econ. Perception ~Likely Vote4Incumbment~")

**XX----------------------------------------------------------------------XX**

* Run logistic regression models for pocketbook economic voting
logit vote_biden_trump inflation_behavior_index gender log_age educ income White Black hispanic democrat republican student full_time part_time unemployed retired big_city demIBR if complete_case [pweight=weight],or
predict phat_pocketbook, p

* Run logistic regression models for sociotropic economic voting
logit vote_biden_trump dem_not_econ gender log_age educ income White Black hispanic democrat republican student full_time part_time unemployed retired big_city dem_not_econ_dem  if complete_case [pweight=weight],or
predict phat_sociotropic, p

* Generate ROC curves
roctab vote_biden_trump phat_pocketbook, nograph
local auc_pocketbook = r(area)

roctab vote_biden_trump phat_sociotropic, nograph
local auc_sociotropic = r(area)

* Combine ROC curves and display them in one graph
roccomp vote_biden_trump phat_pocketbook phat_sociotropic, graph title("ROC Curves Predicting Likelihood~Voting for Incumbent") ///
ylabel(0(0.25)1, angle(horizontal)) xlabel(0(0.25)1) ///
ytitle("Sensitivity (True Positive Rate)") xtitle("1 - Specificity (False Positive Rate)") ///
legend(order(1 "Pocketbook Model (AUC = `auc_pocketbook')" 2 "Sociotropic Model (AUC = `auc_sociotropic')"))


XX----------------------------------------------------------------------XX**

* Full logistic regression model for Pocketbook Economic Voting
logit vote_biden_trump inflation_behavior_index gender log_age educ income White Black hispanic democrat republican student full_time part_time unemployed retired big_city demIBR if complete_case [pweight=weight],or
* Generate predicted probabilities
predict phat_pocketbook, p
* Create deciles of predicted probabilities
xtile decile_pocketbook = phat_pocketbook, n(10)
* Calculate observed proportions of the outcome within each decile
egen mean_phat_pocketbook = mean(phat_pocketbook), by(decile_pocketbook)
egen mean_observed_pocketbook = mean(vote_biden_trump), by(decile_pocketbook)
* Save the calibration plot for the Pocketbook model
twoway (scatter mean_observed_pocketbook mean_phat_pocketbook) ///
       (line mean_observed_pocketbook mean_phat_pocketbook, sort), ///
       title("Calibration Plot for Pocketbook Model") ///
       xtitle("Predicted Probability") ///
       ytitle("Observed Proportion") ///
       legend(off) ///
       xline(0.5) yline(0.5) ///
       saving(pocketbook_plot, replace)

* Full logistic regression model for Sociotropic Economic Voting
logit vote_biden_trump dem_not_econ gender log_age educ income White Black hispanic democrat republican student full_time part_time unemployed retired big_city dem_not_econ_dem  if complete_case [pweight=weight],or
* Generate predicted probabilities
predict phat_sociotropic, p
* Create deciles of predicted probabilities
xtile decile_sociotropic = phat_sociotropic, n(10)
* Calculate observed proportions of the outcome within each decile
egen mean_phat_sociotropic = mean(phat_sociotropic), by(decile_sociotropic)
egen mean_observed_sociotropic = mean(vote_biden_trump), by(decile_sociotropic)
* Save the calibration plot for the Sociotropic model
twoway (scatter mean_observed_sociotropic mean_phat_sociotropic) ///
       (line mean_observed_sociotropic mean_phat_sociotropic, sort), ///
       title("Calibration Plot for Sociotropic Model") ///
       xtitle("Predicted Probability") ///
       ytitle("Observed Proportion") ///
       legend(off) ///
       xline(0.5) yline(0.5) ///
       saving(sociotropic_plot, replace)

* Combine the calibration plots side by side
graph combine pocketbook_plot.gph sociotropic_plot.gph, ///
       title("Calibration Plots for Pocketbook and Sociotropic Models") 
	   
	   //XX----------------------------------------------------------------------XX**
	   
	   * Model 3: Pocketbook Multivariate-ctrl variables interactionterms for Students, Part-time, and Unemployed**
logit vote_biden_trump inflation_behavior_index gender log_age educ income White Black hispanic democrat republican student full_time part_time unemployed retired big_city demIBR if complete_case [pweight=weight], or
estimates store Model3

* Predict Margins and Graphs for Students
margins, at(student=(0 1))
marginsplot, ytitle("Pr(Vote for Incumbent)") xtitle("Student (0=No, 1=Yes)") title("Student Impact on Likely Vote for Incumbent (Pocketbook)") name(plot1, replace)

* Predict Margins and Graphs for Part-time Workers
margins, at(part_time=(0 1))
marginsplot, ytitle("Pr(Vote for Incumbent)") xtitle("Part-time (0=No, 1=Yes)") title("Part-time Impact on Likely Vote for Incumbent (Pocketbook)") name(plot2, replace)

* Predict Margins and Graphs for Unemployed Individuals
margins, at(unemployed=(0 1))
marginsplot, ytitle("Pr(Vote for Incumbent)") xtitle("Unemployed (0=No, 1=Yes)") title("Unemployed Impact on Likely Vote for Incumbent (Pocketbook)") name(plot3, replace)

* Combine all three Pocketbook plots
graph combine plot1 plot2 plot3, col(3) title("Impact of Student, Part-time, and Unemployed on Incumbent Vote (Pocketbook)")


* Model 6: Sociotropic with Interaction for Students, Part-time, and Unemployed**
logit vote_biden_trump dem_not_econ gender log_age educ income White Black hispanic democrat republican student full_time part_time unemployed retired big_city dem_not_econ_dem if complete_case [pweight=weight], or
estimates store Model6

* Predict Margins and Graphs for Students
margins, at(student=(0 1))
marginsplot, ytitle("Pr(Vote for Incumbent)") xtitle("Student (0=No, 1=Yes)") title("Student Impact on Likely Vote for Incumbent (Sociotropic)") name(plot4, replace)

* Predict Margins and Graphs for Part-time Workers
margins, at(part_time=(0 1))
marginsplot, ytitle("Pr(Vote for Incumbent)") xtitle("Part-time (0=No, 1=Yes)") title("Part-time Impact on Likely Vote for Incumbent (Sociotropic)") name(plot5, replace)

* Predict Margins and Graphs for Unemployed Individuals
margins, at(unemployed=(0 1))
marginsplot, ytitle("Pr(Vote for Incumbent)") xtitle("Unemployed (0=No, 1=Yes)") title("Unemployed Impact on Likely Vote for Incumbent (Sociotropic)") name(plot6, replace)

* Combine all three Sociotropic plots
graph combine plot4 plot5 plot6, col(3) title("Impact of Student, Part-time, and Unemployed on Incumbent Vote (Sociotropic)")
